package com.xgp.labblogaffter.Controller;

import com.xgp.labblogaffter.mapper.MsgMapper;
import com.xgp.labblogaffter.mapper.MsgMapperExt;
import com.xgp.labblogaffter.model.Msg;
import com.xgp.labblogaffter.model.MsgExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class MsgController {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    MsgMapper msgMapper;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    MsgMapperExt msgMapperExt;

    /**
     * 根据id删除评论
     * @param sid
     * @return
     */
    @PostMapping("/deleteMsg")
    public boolean deleteMsg(String sid) {
        try {
            int flag = msgMapper.deleteByPrimaryKey(Integer.parseInt(sid));
            if(flag == 1) {
                return true;
            }
            return false;
        }catch (Exception e) {
            return false;
        }

    }

    @GetMapping("/findMsgAllByTime")
    public List<Msg> findMsgAllByTime() {
        try {

            MsgExample msgExample = new MsgExample();
            msgExample.setOrderByClause("time desc");

            return msgMapper.selectByExample(msgExample);
        }catch (Exception e) {
            return null;
        }

    }

    @GetMapping("/findMsgAll")
    public List<Msg> findMsgAll() {
        try {
            return msgMapperExt.findMsgAll();
        }catch (Exception e) {
            return null;
        }

    }

    /**
     * 放回留言数量
     * @return
     */
    @GetMapping("/MsgCount")
    public long MsgCount() {
        try {
            return msgMapperExt.MsgCount();
        }catch (Exception e) {
            return 0;
        }
    }
}
